# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import re
from utils.HttpUtils import HttpUtils

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
高德--道路拥堵排行
https://report.amap.com/ajax/roadRank.do
"""


class Test_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
            "X-Requested-With": "XMLHttpRequest"
        }

    # 根据城市查询
    def get_contents(self):
        postData = {
            "roadType": "0",
            "timeType": "0",
            "cityCode": "440300"
        }

        url = "https://report.amap.com/ajax/roadRank.do"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        data_json = json.loads(html.text)
        for d in data_json['tableData']:
            dict_data = dict()
            dict_data['number'] = d['number']  # 排名
            dict_data['dir'] = d['dir']  # 方向
            dict_data['name'] = d['name']  # 道路
            dict_data['delayTime'] = d['delayTime']  # 延迟时间
            dict_data['index'] = d['index']  # 拥堵延时指数
            dict_data['speed'] = d['speed']  # 速度
            dict_data['travelTime'] = d['travelTime']  # 旅行时间
            dict_data['length'] = d['length']  # 多长距离
            print(str(dict_data))

    # 智能 CT 实时诊断榜
    def get_all(self):
        postData = {
        }
        heads = {
            "cookie": "user_unique_id=8b04ffae6e64889f016ed534f6760484; SESSION=4d947a53-ddf2-4ca9-b3da-cb0a92dcb0ee; UM_distinctid=16ed5350d015d3-0c383937d2bfe6-2393f61-144000-16ed5350d02aa2; guid=49d2-9a08-11cc-ecbe; CNZZDATA1256662931=832547544-1575534525-https%253A%252F%252Freport.amap.com%252F%7C1575534525; user_unique_id=8b04ffae6e64889f016ed536cc1e0485; _uab_collina=157553516909753967394025; key=9d3a96d20fdc76f53eee2b0a4bb6b96a",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
            "x-requested-with": "XMLHttpRequest"
        }
        url = "https://report.amap.com/diagnosis/rank.do"
        html = requests.get(url, headers=heads, verify=False)
        data_json = json.loads(html.text)
        for d in data_json:
            dict_data = dict()
            dict_data['cityName'] = d['cityName']  # 城市名称
            dict_data['healthValue'] = str(d['healthValue']).format(".2%")  # 健康指数
            dict_data['idx'] = d['idx']  # 拥堵延时指数
            dict_data['realSpeed'] = d['realSpeed']  # 平均速度
            print(str(dict_data))


if __name__ == '__main__':
    test_get = Test_Get()
    test_get.get_all()